var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery;
		
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
});

var itemArr = [];
//获取详情
var checkId = getQueryVariable("id");
if (checkId != false) {
	$.ajax({
		url: url + 'checkInfo/getInfo',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			Token: salt,
			id : checkId,
			type : 1
		},
		success: function(data) {
			if (data.code == 0) {
				initDetailItem(data.data);
			} else {
				layer.msg(data.msg);		
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
} else {
	setTimeout(function(){
		layer.msg("详情id为空");
		setTimeout(function(){
			History.go(-1);
		}, 1000);
	}, 1000);
}

//显示
function initItemDetailShow(){
	var tbodyHtml = "";
	for (var i = 0;i < itemArr.length;i++) {
		if (itemArr[i].isShow == 1) {
			var rightTab = 1;//右侧添加检查项目按钮合并行数
			var contentHtml = "";//检查项目内容html
			//内容框list
			for (var j = 0;j < itemArr[i].list.length;j++) {
				if (itemArr[i].list[j].isShow == 1) {
					/*-------------检查项目内容.start-------------*/
					contentHtml += "<tr>" + 
										"<td>" + 
										"</td>" + 
										"<td colspan=\"3\" style=\"padding-right: 0px;\">" + 
											"<div class=\"layui-col-md12 ctd\">" + 
												"<div class=\"layui-col-md12 ctdd\">" + 
													"<div class=\"layui-col-md1 ctdlb\"><label>检查内容</label></div>" + 
													"<label class=\"ctdlb\">" + itemArr[i].list[j].content.split("\n").join("<br />") + "</label>"+
												"</div>" + 
												"<div class=\"layui-col-md12 ctdd\">" + 
													"<div class=\"layui-col-md1 ctdlb\"><label>依据标准</label></div>" + 
													"<label class=\"ctdlb\">" + itemArr[i].list[j].rule.split("\n").join("<br />") + "</label>"+
												"</div>" + 
											"</div>" + 
										"</td>" + 
									"</tr>";
					/*-------------检查项目内容.end-------------*/
					//按钮合并行数+1
					++rightTab;
				}
			}
		
			/*-------------检查项目.start-------------*/
				tbodyHtml += "<tr>" +
								"<td class=\"layui-bg-green\">" +
									"<div class=\"layui-input-inline ftdl\">" +
										"<label class=\"item-nav\">检查项目</label>" +
									"</div>" +
								"</td>" +
								"<td colspan=\"3\" class=\"layui-bg-green\">" +
									"<label style=\"color: white;\">" + itemArr[i].name + "——"+itemArr[i].unit+"</label>"
								"</td>" +
								"<td rowspan=\"" + rightTab + "\">" +
								"</td>" +
							 "</tr>";
				tbodyHtml += contentHtml;
			/*-------------检查项目.end-------------*/
		}
	}
	$("#addTbody").append(tbodyHtml);
}

//初始化详情页面数据
function initDetailItem(checkInfo){
	$("#checkName").html(checkInfo.name);
	$("#applyUnit").html(checkInfo.applyEntName);
	var typeName = "";
	if (checkInfo.type == 1) {
		typeName = "日常检查";
	} else if (checkInfo.type == 2) {
		typeName = "专项检查";
	} else if (checkInfo.type == 3) {
		typeName = "季度检查";
	}
	$("#type").html(typeName);
	
	for (var i = 0;i < checkInfo.checkItems.length;i++) {
		itemArr[i] = {isShow : 1,id : "",name : "",unit : "",list : null};
		itemArr[i].id = checkInfo.checkItems[i].id;
		itemArr[i].name = checkInfo.checkItems[i].name;
		itemArr[i].unit = checkInfo.checkItems[i].unit;
		itemArr[i].list = JSON.parse(checkInfo.checkItems[i].contentsAndRules);
	}
	initItemDetailShow();
}
	
//导出excel
$(".export_btn").click(function() {
	
			$.post(url + 'checkInfo/exportInfo', {
				Token: salt,
				id: checkId
			}, function(result) {
				window.open(url + "file" + result.data);
			});
	
});